package com.onec.service.api.dto.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.onec.service.api.enums.ResultCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 数据返回：
 *
 * @author ONEC
 * @since 2019-07-16
 */

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseData<T> implements Serializable {

    private final static Long serialVersionUID = 1111111111111111L;

    private String sign;

    private String charset;

    private String agentNo;

    private String source;

    private Integer code;

    private String message;

    private String token;

    private T bizContent;

    public static ResponseData ok() {
        return new ResponseData(ResultCodeEnum.SUCCESS);
    }

    public static ResponseData error() {
        return new ResponseData(ResultCodeEnum.FAILED);
    }

    public static ResponseData<String> error(String message) {
        ResultCodeEnum code = ResultCodeEnum.FAILED;
        return new ResponseData<>(code, message);
    }

    public static ResponseData<String> error(ResultCodeEnum code) {
        return new ResponseData<>(code, "");
    }

    public ResponseData(ResultCodeEnum resultCode) {
        setResultCode(resultCode);
    }

    public ResponseData(ResultCodeEnum resultCode, T data) {
        setResultCode(resultCode);
        this.bizContent = data;
    }

    public void setResultCode(ResultCodeEnum resultCodeEnum) {
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMessage();
    }

    public static <T> ResponseData<T> success() {
        return success(null);
    }

    public static <T> ResponseData<T> success(T data) {
        ResultCodeEnum rce = ResultCodeEnum.SUCCESS;
        return new ResponseData(rce, data);
    }

    public boolean isSuccess() {
        return ResultCodeEnum.SUCCESS.getCode().equals(this.code);
    }


}
